package com.weibo.dip.docker;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.ContainerPort;
import com.github.dockerjava.core.DockerClientBuilder;

import java.util.List;

/** Created by haisen on 2018/6/26. */
public class DockerContainerinfo {

  private static DockerClient dockerClient = DockerClientBuilder.getInstance().build();

  public static void showContainerinfo() {
    List<Container> containers = dockerClient.listContainersCmd().exec();
    for (Container container : containers) {
      System.out.print("id " + container.getId() + "  ");
      System.out.print("image " + container.getImage() + "  ");
      System.out.print("imageid " + container.getImageId() + "  ");
      String[] names = container.getNames();
      System.out.print("name ");
      for (String name : names) {
        System.out.print(name + "    ");
      }
      System.out.print("ports   ");
      ContainerPort[] containerPorts = container.getPorts();
      for (ContainerPort containerPort : containerPorts) {
        System.out.print(
            containerPort.getIp()
                + "  "
                + containerPort.getType()
                + "  "
                + containerPort.getPrivatePort()
                + "  "
                + containerPort.getPublicPort());
      }
      System.out.println();
    }
  }

  public static void main(String[] args) {
    DockerContainerinfo.showContainerinfo();
  }
}
